package com.jinxin.platform.cdfaceacs.utils;

import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.cdfaceacs.bean.enums.ResponseCode;
import com.jinxin.platform.cdfaceacs.common.Constant;
import org.springframework.util.StringUtils;

import java.util.regex.Pattern;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-02-27
 * @Author: Lingnan
 **/
public class CommonUtil {

    public static String getDataFromResult(String result){
        //{"r2":"开门成功","code":"0","r1":"true"}
        if(StringUtils.isEmpty(result)){return "";}
        JSONObject returnJson = JSONObject.parseObject(result);
        if(returnJson == null){return "";}
        return returnJson.getString("r2");
    }
    public static String getErrorMsgByResult(String result){
        //{"code":"-2","message":"应答超时"}
        if(StringUtils.isEmpty(result)){return "";}
        JSONObject returnJson = JSONObject.parseObject(result);
        if(returnJson == null){
            return "";
        }
        return returnJson.getString("r2");
    }

    public static boolean isSucceed(String result){
        if(StringUtils.isEmpty(result)){return false;}
        JSONObject returnJson = JSONObject.parseObject(result);
        if(returnJson == null){
            return false;
        }
        String responseCode = returnJson.getString("code");
        return "0".equals(responseCode);
    }

    public static boolean isBase64(String str) {
        String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
        return Pattern.matches(base64Pattern, str);
    }

    public static void main(String[] args) {
//        System.out.println(isSucceed("{r2: \"password fail\", code: \"0\", r1: \"false\"}"));
//        System.out.println(isSucceed("{\"code\":\"0\",\"r1\":\"{\\\"id\\\":\\\"1403249378705027072\\\",\\\"idcardNum\\\":\\\"\\\",\\\"IDNumber\\\":\\\"\\\",\\\"name\\\":\\\"认为如\\\"}\"}"));
        String faceData = "";
        faceData = faceData.replaceFirst(Constant.IMG_BASE64_PREFIX, "");
        System.out.println(faceData);
    }

}
